<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>隧道运维系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <link rel="stylesheet" href="style/admin.css" media="all">


</head>
<body class="layui-layout-body">

<div id="LAY_app">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <!-- 头部区域 -->
            <ul class="layui-nav layui-layout-left">
                <li class="layui-nav-item layadmin-flexible" lay-unselect>
                    <a href="javascript:;" layadmin-event="flexible" title="侧边伸缩">
                        <i class="layui-icon layui-icon-shrink-right" id="LAY_app_flexible"></i>
                    </a>
                </li>
            </ul>
            <ul class="layui-nav layui-layout-right" lay-filter="layadmin-layout-right">
                <li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <a lay-href="/my-task.html" title="我的工作">
                        <i class="layui-icon layui-icon-notice"></i>
                        <!-- 如果有新消息，则显示小圆点 -->
                        <span id="pending-tasks" class="layui-badge-dot"></span>
                    </a>
                </li>
                <li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <a target="_blank" href="/screen/scada/main.html"
                       title="数据大屏">
                        <i class="layui-icon layui-icon-console"></i>
                    </a>
                </li>
                <li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <a href="javascript:;" layadmin-event="fullscreen" title="全屏">
                        <i class="layui-icon layui-icon-screen-full"></i>
                    </a>
                </li>
                <li class="layui-nav-item" lay-unselect>
                    <a href="javascript:;">
                        <cite id="current-user"></cite>
                    </a>
                    <dl class="layui-nav-child">
                        <dd><a lay-href="profile.html">基本资料</a></dd>
                        <dd><a lay-href="change-password.html">修改密码</a></dd>
                        <hr>
                        <dd style="text-align: center;"><a id="logout-btn" lay-href="#">退出</a></dd>
                    </dl>
                </li>
            </ul>
        </div>

        <!-- 侧边菜单 -->
        <div class="layui-side layui-side-menu">
            <div class="layui-side-scroll">
                <div class="layui-logo" lay-href="#">
                    <span>隧道运维系统</span>
                </div>
                <ul class="layui-nav layui-nav-tree" lay-shrink="all" id="LAY-system-side-menu"
                    lay-filter="layadmin-system-side-menu">
                </ul>
            </div>
        </div>

        <!-- 页面标签 -->
        <div class="layadmin-pagetabs" id="LAY_app_tabs">
            <div class="layui-icon layadmin-tabs-control layui-icon-prev" layadmin-event="leftPage"></div>
            <div class="layui-icon layadmin-tabs-control layui-icon-next" layadmin-event="rightPage"></div>
            <div class="layui-icon layadmin-tabs-control layui-icon-down">
                <ul class="layui-nav layadmin-tabs-select" lay-filter="layadmin-pagetabs-nav">
                    <li class="layui-nav-item" lay-unselect>
                        <a href="javascript:;"></a>
                        <dl class="layui-nav-child layui-anim-fadein">
                            <dd layadmin-event="closeThisTabs"><a href="javascript:;">关闭当前标签页</a></dd>
                            <dd layadmin-event="closeOtherTabs"><a href="javascript:;">关闭其它标签页</a></dd>
                            <dd layadmin-event="closeAllTabs"><a href="javascript:;">关闭全部标签页</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
            <div class="layui-tab" lay-unauto lay-allowClose="true" lay-filter="layadmin-layout-tabs">
                <ul class="layui-tab-title" id="LAY_app_tabsheader">
                    <li lay-id="home/console.html" lay-attr="home/console.html" class="layui-this"><i
                            class="layui-icon layui-icon-home"></i></li>
                </ul>
            </div>
        </div>

        <!-- 主体内容 -->
        <div class="layui-body" id="LAY_app_body">
            <div class="layadmin-tabsbody-item layui-show">
                <iframe src="" frameborder="0" class="layadmin-iframe"></iframe>
            </div>
        </div>

        <!-- 辅助元素，一般用于移动设备下遮罩 -->
        <div class="layadmin-body-shade" layadmin-event="shade"></div>
    </div>
</div>

<script type="text/html" id="menu-tpl">
    {{#  layui.each(d, function(index, item){ }}
    <li class="layui-nav-item {{#  if(index === 0){ }} layui-nav-itemed{{#  } }}">
        <a href="javascript:;" lay-tips="{{item.name}}" lay-direction="2">
            <i class="{{item.icon}}"></i>
            <cite>{{item.name}}</cite>
        </a>
        <dl class="layui-nav-child">
            {{# layui.each(item.children, function(index, item){ }}
            <dd>
                <a lay-href="{{item.url}}">{{item.name}}</a>
            </dd>
            {{# }); }}
        </dl>
    </li>
    {{#  }); }}
</script>
<script src="layui/layui.js"></script>
<script src="js/jquery.min.js"></script>
<script>

    var lay = layui.config({
        base: '/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    });
    lay.use(['laytpl', 'layer'], function () {
        var laytpl = layui.laytpl;
        var layer = layui.layer;

        checkPendingTasks();

        function checkPendingTasks() {
            $.get('/tasks/pendings-count', function (result) {
                if (result.data > 0) {
                    $('#pending-tasks').show();
                } else {
                    $('#pending-tasks').hide();
                }
            });
        }

        //loadProjects();

        function loadProjects() {
            $.get('/users/projects', function (result) {
                let data = result.data;
                for (let i in data) {
                    let project = data[i];
                    $('#projects').append('<dd><a href="#" data-project-id="' + project.id + '" data-project-name="' + project.name + '" data-is-guest="' + project.isGuest + '">' + project.name + '</a></dd>');
                }
                $('#projects dd a').on('click', function (e) {
                    let projectId = e.target.dataset.projectId;
                    let projectName = e.target.dataset.projectName;
                    let isGuest = e.target.dataset.isGuest;
                    $('#project').text(projectName);
                    sessionStorage.setItem("projectId", projectId);
                    sessionStorage.setItem("projectName", projectName);
                    sessionStorage.setItem("isGuest", isGuest);
                    var iframes = document.getElementsByTagName("iframe");
                    for (var i = 1; i < iframes.length; i++) {
                        iframes[i].contentWindow.location.reload();
                    }
                });
                if (data.length > 0) {
                    var item = data[0];
                    $('#project').text(item.name);
                    sessionStorage.setItem("projectId", item.id);
                    sessionStorage.setItem("projectName", item.name);
                    sessionStorage.setItem("isGuest", item.isGuest);
                } else {
                    layer.msg('您目前还没有运维项目，请联系负责人添加');
                    sessionStorage.setItem("projectId", '0');
                    sessionStorage.setItem("projectName", '');
                    sessionStorage.setItem("isGuest", '1');
                }
            });
        }

        //加载当前登录人
        $.get('users/current', function (result) {
            $('#current-user').text(result.data.name);
        });
        //加载登录人菜单
        $.get('users/current/menus', function (result) {
            var content = laytpl($('#menu-tpl').html()).render(result.data);
            $('#LAY-system-side-menu').html(content);
            lay.use('index');
        });

        $('#logout-btn').on('click', function () {
            $.get('users/logout', function () {
                window.location.href = 'login.html';
            });
        });
    });
</script>

</body>
</html>


